"""
进度管理服务
"""
import threading
from typing import Dict, Optional

class ProgressService:
    """处理进度管理服务"""
    
    def __init__(self):
        """初始化进度服务"""
        self._progress_data: Dict[str, dict] = {}
        self._lock = threading.Lock()
    
    def init_progress(self, task_id: str, total: int = 0) -> None:
        """
        初始化任务进度
        
        Args:
            task_id: 任务ID
            total: 总条目数
        """
        with self._lock:
            self._progress_data[task_id] = {
                'total': total,
                'processed': 0,
                'matches': 0,
                'status': 'starting'
            }
    
    def update_progress(self, task_id: str, processed: int = None, 
                       matches: int = None, status: str = None) -> None:
        """
        更新任务进度
        
        Args:
            task_id: 任务ID
            processed: 已处理数量
            matches: 匹配数量
            status: 状态
        """
        with self._lock:
            if task_id in self._progress_data:
                if processed is not None:
                    self._progress_data[task_id]['processed'] = processed
                if matches is not None:
                    self._progress_data[task_id]['matches'] = matches
                if status is not None:
                    self._progress_data[task_id]['status'] = status
    
    def increment_processed(self, task_id: str) -> None:
        """
        增加已处理数量
        
        Args:
            task_id: 任务ID
        """
        with self._lock:
            if task_id in self._progress_data:
                self._progress_data[task_id]['processed'] += 1
    
    def increment_matches(self, task_id: str) -> None:
        """
        增加匹配数量
        
        Args:
            task_id: 任务ID
        """
        with self._lock:
            if task_id in self._progress_data:
                self._progress_data[task_id]['matches'] += 1
    
    def get_progress(self, task_id: str) -> Optional[dict]:
        """
        获取任务进度
        
        Args:
            task_id: 任务ID
        
        Returns:
            dict: 进度信息，如果任务不存在则返回None
        """
        with self._lock:
            return self._progress_data.get(task_id, {}).copy()
    
    def set_excel_filename(self, task_id: str, filename: str) -> None:
        """
        设置Excel文件名
        
        Args:
            task_id: 任务ID
            filename: Excel文件名
        """
        with self._lock:
            if task_id in self._progress_data:
                self._progress_data[task_id]['excel_filename'] = filename
    
    def get_excel_filename(self, task_id: str) -> Optional[str]:
        """
        获取Excel文件名
        
        Args:
            task_id: 任务ID
        
        Returns:
            str: Excel文件名，如果不存在则返回None
        """
        with self._lock:
            return self._progress_data.get(task_id, {}).get('excel_filename')
    
    def is_task_exists(self, task_id: str) -> bool:
        """
        检查任务是否存在
        
        Args:
            task_id: 任务ID
        
        Returns:
            bool: 任务是否存在
        """
        with self._lock:
            return task_id in self._progress_data
    
    def is_task_completed(self, task_id: str) -> bool:
        """
        检查任务是否完成
        
        Args:
            task_id: 任务ID
        
        Returns:
            bool: 任务是否完成
        """
        with self._lock:
            return self._progress_data.get(task_id, {}).get('status') == 'completed'

# 全局进度服务实例
progress_service = ProgressService()

